package com.tang.multi.v1;

import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @author Tang
 * @classname Acceptor
 * @description [ 请求接收 ]
 * @date 2021/8/10 14:00
 */
public class Acceptor implements Runnable{

    private ServerSocketChannel serverSocketChannel;

    private Selector selector;

    public Acceptor(Selector selector, ServerSocketChannel serverSocketChannel) {
        this.serverSocketChannel = serverSocketChannel;
        this.selector = selector;
    }

    @Override
    public void run() {
        try {
            SocketChannel socketChannel = this.serverSocketChannel.accept();
            socketChannel.configureBlocking(false);
            SelectionKey selectionKey = socketChannel.register(this.selector, SelectionKey.OP_READ);
            selectionKey.attach(new WorkHandler(socketChannel,selectionKey));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}